import BundleManager from "../manager/BundleManager";
import { JsonAsset } from "cc";
import protobuf from 'protobufjs';
/**
 * cnpm install protobufjs@6.11.4 --save
 */
export default class NetData {
    static pbRoot: protobuf.Root = null;

    static init() {
        console.log("初始化NetData");// 初始化protobuf的Json文件
        return new Promise<void>((resolve) => {
            BundleManager.Instance.loadResType("datas/protoBuffer/proto", JsonAsset).then((content: JsonAsset) => {// assert/resources中的proto.json路径
                console.log(JSON.stringify(content.json)); 
                NetData.pbRoot = protobuf.Root.fromJSON(content.json);
                console.log(NetData.pbRoot);
                resolve();
            });
        });
    }

    static createProtoInstance<T>(name: string): T {
        let messageCls:protobuf.Type | any = <any>this.pbRoot.lookupType("proto" + "." + name);// shell脚本打包时包内的package根内容 proto
        // console.log(messageCls);
        let msg = messageCls.create();
        msg.protoCls = messageCls;
        msg._protoName = name;
        return msg;
    }

}